<?php

namespace app\api\controller\Product;

use think\Controller;

class Purchase extends Controller
{

    public function __construct()
    {
        parent::__construct();

        $this->CartModel = model('Product.Cart');
        $this->ProductModel = model('Product.Product');
        $this->UserModel = model('User.User');
    }

    // 立即购买
    public function buy()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid',0);

            $proid = $this->request->param('proid',0);

            $product = $this->ProductModel->find($proid);

            if($product['stock'] <= 0)
            {
                $this->error('该商品库存不足');
                exit;
            }

            $user = $this->UserModel->find('$userid');

            if($user['vip'] > 0) 
            {
                $price = $product['vip_price'] > 0 ? $product['vip_price'] : $product['vip_price'];
            }else{
                $price = $product['price'];
            }

            $ProductList = $this->ProductModel->select($proid);

            if(!$ProductList)
            {
                $this->error('该商品不存在');
                exit;
            };

            $data = [
                'ProductList' => $ProductList
            ];

            $this->success('购买成功',null,$data);
        }
    }

}


?>
